package xyz.icxl.android.architecture.ui

import android.content.DialogInterface
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import xyz.icxl.android.architecture.R

/**
 * 基础弹窗
 * @author cxl
 * @date 2021/4/23
 */
abstract class BaseDialogFragment : DialogFragment() {

    val isShowing: Boolean
        get() = dialog?.isShowing == true

    abstract fun layoutId(): Int

    abstract fun doSomething(rootView: View)

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(layoutId(), container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        doSomething(view)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NORMAL, R.style.fullscreenDialog)
    }

    private var dismissListener: (() -> Unit)? = null
    fun registerDismissListener(action:()->Unit){
        dismissListener = action
    }

    override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)
        dismissListener?.invoke()
    }


}